<?php
/**
 *
 * Author: lirong
 * Date: 2017/10/22
 * Time: 11:27
 */

namespace Www\Controller;

use Think\Controller;

define('XS_APP_ROOT',CONF_PATH);

class XunController extends Controller {

    private $xs =null;

    public function __construct(){
        parent::__construct();
        Vendor('Xun.lib.XS');
        $this->xs =new \XS('dtzs');
    }

    public function index(){

        $this->display();
    }

    public function batch_index(){

     //   $data =M('portalPost')->field('id,parent_id,user_id,is_top,recommended,post_hits,create_time,post_title,post_keywords,post_excerpt,post_content')->select();
        $data =M('portalPost')->select();
        $index =$this->xs->getIndex();
        $doc =new \XSDocument();

        foreach ($data as $value){
            $doc->setFields($value);
            $index->add($doc);
        }

    //    $this->ajaxReturn($data);


    }

    public function batch_hot(){
        $search =$this->xs->getSearch();
        $data =$search->getHotQuery(10);

        print_r($data);

    }

    public function batch_edit(){

    }

    public function batch_delete(){
        $id =I('get.id','0','intval');
        if (empty($id)){
            $this->ajaxRetrun(['code'=>1002,'msg'=>'ID不能为空!']);
        }

        $index = $this->xs->getIndex();
        $index->del($id);

        $this->ajaxReturn(['code'=>1001,'msg'=>'操作成功!']);

    }

    public function batch_clear(){

        $index =$this->xs->getIndex();
        $result = $index->clean();
        print_r($result);
    }


    public function batch_search(){

        $keyword =I('get.key','','trim');
        $p = I('get.page','1','intval');
        $listrow=10;

        $page =($p-1)*$listrow;

        if (empty($keyword)){
            return false;
        }

        $search = $this->xs->getSearch();

        $docs =$search->setQuery($keyword)->setLimit($listrow,$page)->setSort('create_time',true)->search();
        $count =$search->count();
        $data =[];
        foreach ($docs as $k => $doc){
            $data[$k]['id']=$doc->id;
            $data[$k]['title'] =$doc->post_title;
            $data[$k]['create_time'] =date('Y-m-d H:i:s',$doc->create_time);
        }

        $this->ajaxReturn($data);
    }


    public function getRelatedQuery(){
        $keyword =I('get.key','','trim');
        $search =$this->xs->getSearch();
        $data =$search->getRelatedQuery($keyword,10);


        print_r($data);

    }



}